#include<16f877a.h>
#device ADC=10
#include<stdio.h>

#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=16000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#define row1() init_lcd(0x80)	/* Begin at Line 1 */
#define row2() init_lcd(0xC0)   /* Begin at Line 2 */
#define LCD_Clear() init_lcd(0x01)	/* Clear display LCD */
unsigned char w[] = { 0x38,0x0c,0x01,0x06,0x80};
unsigned int adc_val=0;


/*======================================================LCD Initialisation============================================*/
/*void init_lcd(unsigned char j)
{
   output_e (0x00);
   output_d (j);
   output_e (0x02);
   delay_ms (2);
   output_e (0x00);
   delay_ms (2);
}
/*=======================================================Display Function=============================================*/
/*void display(char s)
{
   output_e (0x01);
   output_d (s);
   output_e (0x03);
   delay_ms (2);
   output_e (0x01);
   delay_ms (2);
}
/*=======================================================Functions For ROW============================================*/
/*void Lcd_init()
{
unsigned char i=0;
   for( i = 0 ; i < 6 ; i++ )
   {
      init_lcd (w[i]);
   }
}*/

/*void main()
{
   unsigned int x_dir=0,dec=0,y_dir=0,temperature;
   static int count=0;
   //Lcd_init();
   //row1();
   //display("HEART BEAT");
   setup_port_A(ALL_ANALOG);
   setup_adc(ADC_CLOCK_INTERNAL);


      while(1)
      {
         set_adc_channel(0);
         delay_us(20);
         x_dir=Read_ADC();

         /*set_adc_channel(1);
         delay_us(20);
         y_dir=Read_ADC();

         temperature=(5.0 * y_dir * 10.0) / 126 ;
         printf("\n\rtemp=%d",temperature);
         printf("\n\rheart=%u",x_dir);

         if(temperature>50)
         {
         printf("over temperature");
         }
         delay_ms(500);*/
         /*while(dec<240)
         {

            if(x_dir>60)
            {
            //printf("on");
               count++;

              // delay_ms(100);
            }
         dec++;
         }
         if(count>80)
               {
                  printf("Over Heart Beat");
               }
         printf("\n\r%d",count);
         dec=0;

      }
}
      /*if(count>=50 && count<60)
      {
      count=count+20;
      }
      else if(count<=100 && count>90 )
      {
      count=count-18;
      }
      else if(count>=40 && count<49)
      {
      count=count+28;
      }
      else if(count>=30 && count<39)
      {
      count=count+38;
      }
      else if(count<30)
      {
      count=68;
      }
      else if(count>100)
      {
      count=79;
      }*/



void main()
{
   unsigned char adc_val,adc_val1,adc_val2;
   int y_dir,temperature;
   setup_port_A(ALL_ANALOG);
   setup_adc(ADC_CLOCK_INTERNAL);

 while(1)
   {
     set_adc_channel(0);
     delay_us(20);
     adc_val=Read_ADC();
     printf("t=%u\n\r",adc_val);
     delay_ms(100);
     set_adc_channel(1);
     delay_us(20);
     adc_val1=Read_ADC();
     
     printf("b=%u\n\r",adc_val1);
     delay_ms(100);
     set_adc_channel(2);
     delay_us(20);
     adc_val2=Read_ADC();
     printf("adc=%u\n\r",adc_val2);
     delay_ms(100);
     /*if(adc_val<20)
      {
         adc_val=adc_val+50;
         delay_ms(1500);
         printf("Heart Beat is=%u\n\r",adc_val);
      }
   else if(adc_val>=20 && adc_val<=30)
   {

      adc_val+=58;
      delay_ms(500);
      printf("Heart Beat is=%u\n\r",adc_val);
   }

   else if(adc_val>=31 && adc_val<=40)
   {

     adc_val+=35;
     delay_ms(500);
     printf("Heart Beat is=%u\n\r",adc_val);
 }

   else if(adc_val>=41 && adc_val<=54)
   {

     adc_val+=25;
     delay_ms(500);
     printf("Heart Beat is=%u\n\r",adc_val);
   }
   else if(adc_val>=80 && adc_val<=90)
   {
      adc_val+=5;
      delay_ms(500);
     printf("Heart Beat is=%u\n\r",adc_val);
  // printf("\n\r %uOver Heart Beat",adc_val);
   }

         /*set_adc_channel(1);
         delay_us(20);
         y_dir=Read_ADC();
         temperature=(5.0 * y_dir * 10.0) / 126 ;
         printf("\n\rTemperature is =%d",temperature);
         //printf("\n\rheart=%u",x_dir);

         if(temperature>50)
         {
         printf("over temperature");
         }
         delay_ms(500);*/
}
}

